package com.yyn.Learning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yyn.Learning.entity.EduChapter;
import com.yyn.Learning.entity.EduVideo;
import com.yyn.Learning.entity.Subject.OneLevel;
import com.yyn.Learning.entity.Subject.TwoLevel;
import com.yyn.Learning.mapper.EduChapterMapper;
import com.yyn.Learning.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyn.Learning.service.EduVideoService;
import com.yyn.servicebase.exceptionhandler.CustomException;
import org.apache.poi.xwpf.usermodel.BreakType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author yyn
 * @since 2022-11-22
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    public EduVideoService eduVideoService;

    @Override
    public void saveChapter(EduChapter eduChapter) {
        this.save(eduChapter);
    }

    @Override
    public Boolean removeChapter(String id) {
//        1、查询该篇章id下有无video视频
//        2、有的话需要先删除再删除章节
        LambdaQueryWrapper<EduVideo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(EduVideo::getChapterId, id);
        if (eduVideoService.getOne(lambdaQueryWrapper) != null) {
            throw new CustomException(20001, "该章节不为空");
        } else {
            boolean b = this.removeById(id);//删篇章
//            来到这一步就肯定传过来的是小节id了  删小节
            if(!b){//删篇章失败
                eduVideoService.removeTwo(id);
            }

            return true;
        }
    }

    @Override
    public void updateChapter(EduChapter eduChapter) {
        this.updateById(eduChapter);
    }

    @Override
    public void getAllChapter() {
        this.list();
    }

    @Override
    public EduChapter getOneChapter(String id) {
        LambdaQueryWrapper<EduChapter> eduChapterLambdaQueryWrapper = new LambdaQueryWrapper<>();
        eduChapterLambdaQueryWrapper.eq(EduChapter::getId, id);
        return this.getOne(eduChapterLambdaQueryWrapper);
    }


    /**
     * 1、查询所有章节
     * 2、用流逐个存进一级分类实体类中
     * 3、制定一个最终的list来存放（add）
     * 4、进行二层流遍历 查询所有小节
     * 5、逐个放入二级分类实体类中
     * 6、制定一个二级最终的list来存（add)
     **/

    @Override
    public List getChapterTree(String id) {
//        最终list
        List<OneLevel> finalList = new ArrayList<>();
//        章节
        LambdaQueryWrapper<EduChapter> eduChapterLambdaQueryWrapper = new LambdaQueryWrapper<>();
        eduChapterLambdaQueryWrapper.eq(EduChapter::getCourseId, id);
        List<EduChapter> oneList = this.list(eduChapterLambdaQueryWrapper);
//        小节
        LambdaQueryWrapper<EduVideo> eduVideoLambdaQueryWrapper = new LambdaQueryWrapper<EduVideo>();
        eduVideoLambdaQueryWrapper.eq(EduVideo::getCourseId, id);
        List<EduVideo> twoList = eduVideoService.list(eduVideoLambdaQueryWrapper);

        oneList = oneList.stream().map(item -> {
            ArrayList<TwoLevel> finalTwoLevel = new ArrayList<>();
            OneLevel oneLevel = new OneLevel();
            BeanUtils.copyProperties(item, oneLevel);
            //                    用与tree选择器
            oneLevel.setLabel(oneLevel.getTitle());
            finalList.add(oneLevel);
            twoList.stream().map(item2 -> {
                if (item.getId().equals(item2.getChapterId())) {
                    TwoLevel twoLevel = new TwoLevel();
                    BeanUtils.copyProperties(item2, twoLevel);
//                    用与tree选择器
                    twoLevel.setLabel(twoLevel.getTitle());
                    finalTwoLevel.add(twoLevel);
                    oneLevel.setChildren(finalTwoLevel);
                }
                return item2;
            }).collect(Collectors.toList());

            return item;
        }).collect(Collectors.toList());

        return finalList;
    }

    @Override
    public void removeByCourseId(String courseId) {
        LambdaQueryWrapper<EduChapter> eduChapterLambdaQueryWrapper = new LambdaQueryWrapper<>();
        eduChapterLambdaQueryWrapper.eq(EduChapter::getCourseId,courseId);
        this.remove(eduChapterLambdaQueryWrapper);

    }
}
